package web;

import Utils.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "SessionServlet", value = "/SessionServlet")
public class SessionServlet extends BaseServlet {
    protected void createOrGetSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        response.getWriter().write("Session ID: " + session.getId() + ", New: " + session.isNew());
    }

    protected void setAttr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("k1", "v1");
    }

    protected void getAttr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object k1 = request.getSession().getAttribute("k1");
        response.getWriter().write(k1.toString());
    }

    protected void defaultLife(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int interval = request.getSession().getMaxInactiveInterval();
        response.getWriter().write("interval: " + interval);
    }
    
    protected void setLife(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(3);
        response.getWriter().write("设置成功！");
    }

    protected void deleteNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();
        response.getWriter().write("销毁成功！");
    }
}
